package com.youtills.core.monitor;

import com.youtills.core.lang.InstanceMaker;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ravi
 * Date: 9/5/12
 * Time: 12:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class Monitors{

//    private static final DiagnosticLogger LOG = new DiagnosticLogger(Monitors.class);

    private static final InstanceMaker<IMonitor> INSTANCE_MAKER = new InstanceMaker<IMonitor>(true);

    private static final Monitors instance = new Monitors();

    private final Map<String, IMonitor> SERVICE_MONITORS;


    public static void addServiceMonitor(String serviceMonitorClass){
        addServiceMonitor(INSTANCE_MAKER.getInstanceOf(serviceMonitorClass));
    }

    public static void addServiceMonitor(IMonitor serviceMonitor){

        if(serviceMonitor != null){

            String name = serviceMonitor.getKey();

            if(instance.SERVICE_MONITORS.containsKey(name)){
//                LOG.log("Duplicate service monitor item for " + name + " existing will be replaced.");
            }
            instance.SERVICE_MONITORS.put(name, serviceMonitor);
        }
    }

    public static void removeServiceMonitor(Class serviceMonitorClass){
        if(serviceMonitorClass != null){
            instance.SERVICE_MONITORS.remove(serviceMonitorClass.getName());
        }
    }

    private Monitors() {
        SERVICE_MONITORS = new HashMap<String, IMonitor>();
    }

    public static Collection<IMonitor> getMonitors(){
        return Collections.unmodifiableCollection(instance.SERVICE_MONITORS.values());
    }

    public static IMonitor getMonitor(String key){
        return instance.SERVICE_MONITORS.get(key);
    }

}